package com.example.yam.greenapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Display;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ShdzActivity extends AppCompatActivity {
    //常量
    private final String TAG="ShdzActivity";

    //变量
    private String myurl = "Green/index.php/indexg/shdz/uid/";

    //控件
    private ListView listView = null;
    private Button btn = null;
    //列表
    private List<Shdz> shdzList = new ArrayList<Shdz>();

    //适配器
    private ShdzAdapter shdzAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shdz);

        //工具栏--返回
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        this.listView = (ListView) super.findViewById(R.id.shdz_list);
        this.btn = (Button) super.findViewById(R.id.shdz_post);
        this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MyApplication.myshdz = shdzList.get(position);
                shdzAdapter.Select(position);
            }
        });
        this.btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent it = new Intent(ShdzActivity.this, DisplayActivity.class);
                startActivity(it);
            }
        });
        getMessage();
    }
    //初始化
    public void getMessage(){
        String url = MyApplication.localhost+this.myurl+MyApplication.user.getId();
        Log.v(TAG,url);
        VolleyGetString(url);
    }
    //使用volley获取数据
    private void VolleyGetString(String url){
        RequestQueue mQueue = MyApplication.getHttpQueue();
        Log.d(TAG, "开始了");
        StringRequest stringRequest = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {//正确搜索到数据
                        parseJSONobject(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, error.getMessage(), error);
            }
        });
        mQueue.add(stringRequest);
    }
    //json解析
    private void parseJSONobject(String ss){
        try{
            Log.d(TAG,"数据是"+ss);
            JSONObject jsb = new JSONObject(ss);
            JSONArray jab = (JSONArray) new JSONArray(jsb.getString("shdz"));
            shdzList.add(MyApplication.myshdz);
            for(int i=0,len=jab.length();i<len;i++){
                JSONObject jb = jab.getJSONObject(i);
                Shdz shdz = new Shdz(jb.getString("id"),jb.getString("name"),jb.getString("phone"),jb.getString("location"));
                shdzList.add(shdz);
            }
            shdzAdapter= new ShdzAdapter(ShdzActivity.this,R.layout.shdzlist,this.shdzList);
            shdzAdapter.setList(this.shdzList);
            shdzAdapter.Select(0);
            this.listView.setAdapter(shdzAdapter);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
